﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DAL;

namespace BLL.BatchBLL
{
    public class unCrteProdBatchBLL
    {
        private BatchRepository batchRepository = new BatchRepository();
        private WorkOrderRepository workOrderRepository = new WorkOrderRepository();
        private ProductRepository productRepository = new ProductRepository();
        private ProcessFlowRepository processFlowRepository = new ProcessFlowRepository();
        private StationRepository stationRepository = new StationRepository();

        public List<QueryBatchAndWorkOrderItem> QueryBatchAndWorkOrder()
        {
            var batches = batchRepository.GetAllBatches();
            var workOrders = workOrderRepository.GetAllWorkOrders();

            var queryResult = from batch in batches
                              join workOrder in workOrders
                              on batch.WorkOrderId equals workOrder.WorkOrderId
                              select new QueryBatchAndWorkOrderItem
                              {
                                  BatchId = batch.BatchId,
                                  BatchType = batch.BatchType,
                                  Quantity = batch.Quantity,
                                  SubQuantity = batch.SubQuantity,
                                  LockStatus = batch.LockStatus,
                                  WorkOrderNumber = batch.WorkOrderId.ToString(),
                                  TotalProcessId = batch.TotalProcessId,
                                  EventTime = batch.EventTime,
                                  EventUser = batch.EventUser,
                                  EventRemarks = batch.EventRemarks,
                                  EventName = batch.EventName,
                                  ProductId = workOrder.ProductId.ToString(),
                                  Unit = batch.Unit
                              };

            return queryResult.ToList();
        }

        public List<string> GetAllEventTimes()
        {
            var batches = batchRepository.GetAllBatches();
            var eventTimes = batches.Select(batch => batch.EventTime.ToString("yyyy-MM-dd"))
                                    .Distinct()
                                    .ToList();
            return eventTimes;
        }

        public Product GetProductByWorkOrderId(int workOrderId)
        {
            var workOrder = workOrderRepository.GetWorkOrderById(workOrderId);
            if (workOrder != null)
            {
                return productRepository.GetProductById(workOrder.ProductId);
            }
            return null;
        }

        public ProcessFlow GetProcessFlowByFlowId(int flowId)
        {
            return processFlowRepository.GetProcessFlowById(flowId);
        }

        public Station GetStationByStationId(int stationId)
        {
            return stationRepository.GetStationById(stationId);
        }
    }
}